package com.hl.card.modular.packBack.service.impl;

import com.hl.card.common.persistence.dao.DisMemberApplyMapper;
import com.hl.card.common.persistence.dao.DisMemberInfoMapper;
import com.hl.card.common.persistence.model.DisMemberApply;
import com.hl.card.common.persistence.model.DisMemberInfo;
import com.hl.card.common.util.FileUtil;
import com.hl.card.modular.packBack.model.ExcelPartnerApply;
import com.hl.card.modular.packBack.service.PartnerApplyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PartnerApplyServiceImpl implements PartnerApplyService {

    @Resource(name = "disMemberApplyMapper")
    private DisMemberApplyMapper disMemberApplyMapper;

    @Resource(name = "disMemberInfoMapper")
    private DisMemberInfoMapper disMemberInfoMapper;

    @Override
    public void save(DisMemberApply disMemberApply) {
        disMemberApplyMapper.insert(disMemberApply);
    }

    @Override
    public void update(DisMemberApply disMemberApply) {
        disMemberApplyMapper.updateById(disMemberApply);
    }

    @Override
    public List<DisMemberApply> selectList(Map<String, Object> map) {
        List<DisMemberApply> list = disMemberApplyMapper.selectApplyList(map);
        return list;
    }

    @Override
    public DisMemberApply selectById(Integer id) {
        DisMemberApply disMemberApply = disMemberApplyMapper.selectApplyById(id);
        return disMemberApply;
    }

    @Override
    public DisMemberApply selectByPhone(String phone) {
        DisMemberApply disMemberApply = disMemberApplyMapper.selectApplyByPhone(phone);
        return disMemberApply;
    }

    @Override
    public void saveDisMeberInfo(DisMemberInfo disMemberInfo) {
        disMemberInfoMapper.insert(disMemberInfo);
    }

    public String creatOrderNo() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        int a = (int) (Math.random() * 900) + 100;
        String orderNo = format.format(new Date()) + a;
        return "D" + orderNo;
    }

    @Override
    public void excelList(Map<String, Object> map, HttpServletResponse response) throws Exception{
        List<DisMemberApply> list = disMemberApplyMapper.selectApplyExcel(map);
        List<ExcelPartnerApply> result = new ArrayList<>();

        for (int i =0;i<list.size();i++){
            DisMemberApply disMemberApply = list.get(i);
            ExcelPartnerApply excelPartnerApply = new ExcelPartnerApply();
            excelPartnerApply.setZhuangtai(disMemberApply.getZhuangtai());
            excelPartnerApply.setApplyTime(disMemberApply.getApplyTime());
            excelPartnerApply.setUserName(disMemberApply.getUserName());
            excelPartnerApply.setBindingPhone(disMemberApply.getBindingPhone());
            excelPartnerApply.setIdNo(disMemberApply.getIdNo());
            excelPartnerApply.setRecommendationNo(disMemberApply.getRecommendationNo());
            excelPartnerApply.setPayMoney(disMemberApply.getPayMoney());
            excelPartnerApply.setFlowNo(disMemberApply.getFlowNo());
            if(disMemberApply.getPayType().equals("0")){
                excelPartnerApply.setPayType("支付宝");
            }else if(disMemberApply.getPayType().equals("1")){
                excelPartnerApply.setPayType("微信");
            }else if(disMemberApply.getPayType().equals("3")){
                excelPartnerApply.setPayType("线下");
            }else{
                excelPartnerApply.setPayType("其他");
            }
            excelPartnerApply.setPayTime(disMemberApply.getPayTime());
            excelPartnerApply.setExamineTime(disMemberApply.getExamineTime());

            result.add(excelPartnerApply);
        }
        FileUtil.exportExcel(result, "合伙人申请列表", "申请", ExcelPartnerApply.class, "合伙人申请列表.xls",response);
    }
}
